
import okhttp3.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Spider {
    private static final OkHttpClient CLIENT = new OkHttpClient.Builder()
            .cookieJar(new CookieJarManager())
            .build();

    public static void login(){


        /**
         * POST https://www.mosoteach.cn/web/index.php?c=passport&m=account_login
         *
         * account_name=17758963927
         * &user_pwd=taorui..
         * &remember_me=Y
         * &scene=
         * &time=
         * &sign=
         **/
        // 构造请求体参数
        FormBody body = new FormBody.Builder()
                .add("account_name", "17758963927")
                .add("user_pwd", "taorui..")
//                .add("remember_me", "Y")
                .add("scene", "")
                .add("time", "")
                .add("sign", "")
                .build();

        // 构造请求
        Request request = new Request.Builder()
                .url("https://www.mosoteach.cn/web/index.php?c=passport&m=account_login")
                .post(body)
                .build();

        // 请求
        Response response = null;
        try {
            response = CLIENT.newCall(request).execute();
            if(!response.isSuccessful()){
                System.out.println("登录失败");
            }
        } catch (IOException e) {
            System.out.println("请求失败");
        }

    }

    public static void crawl() {
        // 爬取活动列表页
        Request request = new Request.Builder()
                .url("https://www.mosoteach.cn/web/index.php?c=interaction&m=index&clazz_course_id=CD7AE281-4AF8-11EA-9C7F-98039B1848C6")
                .get()
                .build();
        Response response = null;
        try {
            response = CLIENT.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert response != null;
        if(response.isSuccessful()){
            try {
                // 下载所有课堂完成部分的html
                List<String> htmls = resolve(response.body().string());
                //  分析并写入文件
                for (String html : htmls) {
                    Soup.soupAndStore(html);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }
    public static List<String> resolve(String html){
        Document document = Jsoup.parse(html);
        Elements interactionRow = document.getElementsByClass("interaction-row");
        ArrayList<String> htmls = new ArrayList<>();
        interactionRow.forEach(row -> {
            if(row.getElementsByClass("interaction-name").get(0).text().contains("课堂完成部分")){
                String url = row.attr("data-url");
                Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                Response response = null;
                try {
                    response = CLIENT.newCall(request).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                assert response != null;
                if(response.isSuccessful()){
                    try {
                        htmls.add(response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
       return htmls;
    }
}
